package com.wjggy.core.common.dbtools;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Component;

import com.wjggy.core.common.page.Page;

/**
 * 
 *  <p>Description: 数据库Dao层帮助类 </p>
 *  <p>Copyright:Copyright (c) 2012  </p>
 *  <p>Company:wjggy  </p>
 *  @author xiaofan.yu  
 *  @date   2012-1-8 下午10:06:18    
 *  @project  wjggy
 *  @version  1.0.0
 *  @see   
 *  @since 1.6
 */
@Component("daoUtils")
public class DaoUtils{
	@Resource(name = "sqlSessionTemplate") 
	private SqlSessionTemplate  sqlSessionFactory;
	public void setSqlSessionTemplate(SqlSessionTemplate  sqlSessionFactory) {
		this.sqlSessionFactory=sqlSessionFactory;
	}
	/**
	 * 
	 *  @describe 查找对象
	 *  @param statement
	 *  @param parameter
	 *  @return
	 */
	public Object findObject(String statement,Object parameter){
		return this.sqlSessionFactory.selectOne(statement, parameter);
	}
	/**
	 * 
	 *  @describe 新增数据
	 *  @param statement
	 *  @param parameter
	 *  @return
	 */
	public int add(String statement,Object parameter){
		return this.sqlSessionFactory.insert(statement, parameter);
	}
	/**
	 * 
	 *  @describe 删除数据 
	 *  @param statement
	 *  @param parameter
	 *  @return
	 */
	public int delete(String statement,Object parameter){
		return this.sqlSessionFactory.delete(statement, parameter);
	}
	/**
	 * 分页查找
	 * @param statementName
	 * @param parameter
	 * @param rowBounds分页对象
	 * @return
	 */
	public List findRows(String statementName,Object parameter,Page page){
		return   this.sqlSessionFactory.selectList(statementName, parameter,page);
	}
	/**
	 * 不分页进行查找
	 * @param statementName
	 * @param parameter
	 * @return
	 */
	public List findAll(String statementName,Object parameter){
		return   this.sqlSessionFactory.selectList(statementName, parameter);
	}
	/**
	 * 不分页进行查找
	 * @param statementName
	 * @param parameter
	 * @return
	 */
	public List findAll(String statementName){
		return   this.sqlSessionFactory.selectList(statementName);
	}
	/**
	 * 修改对象,并返回修改数目
	 * @param statementName
	 * @param parameterObject
	 * @return
	 */
	public int update(String statementName,Object parameterObject){
			return this.sqlSessionFactory.update(statementName, parameterObject);
	}
	
	/**
	 * 
	 *  @author xiaofan.yu  
	 *  DateTime 2011-12-13 上午10:57:15
	 *  @see 
	 *  @since 获取表主键  
	 *  describe:
	 *  @param tableName
	 *  @return
	 */
	public int findTableKey(String tableName){
			TableMapper tableMap=this.sqlSessionFactory.getMapper(TableMapper.class);
			Integer maxOid=tableMap.findTableKey(tableName);
			if(maxOid==null){
				tableMap.insertTableKey(tableName);
				return 1;
			}else{
				tableMap.updateTableKey(tableName);
				return maxOid+1;
			}
	}
	
	/**
	 * 
	 *  @author xiaofan.yu  
	 *  DateTime 2011-12-13 上午10:57:15
	 *  @see 
	 *  @since 获取表主键  
	 *  describe:
	 *  @param tableName
	 *  @return
	 */
	public String findTableKeyUUID(){
		return UUID.randomUUID().toString().replace("-", "").toUpperCase();
	}
}
